package com.xs.shop.marketing.service.controller;

import com.xs.shop.common.PageResult;
import com.xs.shop.common.Result;
import com.xs.shop.marketing.model.entity.Activity;
import com.xs.shop.marketing.model.entity.PointLog;
import com.xs.shop.marketing.model.vo.PointLogListVO;
import com.xs.shop.marketing.service.service.PointLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xs
 * @since 2025-07-20
 */
@RestController
@Slf4j
@Tag(name = "积分变动记录管理", description = "积分变动记录的CRUD操作")
@RequestMapping("/pointLog")
public class PointLogController {

    @Resource
    private PointLogService pointLogService;


    @PostMapping("add")
    @Operation(summary = "新增积分变动记录", description = "创建一个新的积分变动记录")
    public Result<Object> addPointLog(@RequestBody PointLog pointLog) {
        this.pointLogService.addPointLog(pointLog);
        return Result.success();
    }

    @DeleteMapping("{id}")
    @Operation(summary = "删除积分变动记录", description = "根据ID删除积分变动记录")
    public Result<Object> deletePointLog(@PathVariable("id") @Parameter(description = "记录ID") Long id) {
        this.pointLogService.deletePointLog(id);
        // 管理员端
        return Result.success();
    }

    @PutMapping("update")
    @Operation(summary = "更新积分变动记录", description = "更新积分变动记录")
    public Result<Object> updatePointLog(@RequestBody PointLog pointLog) {
        this.pointLogService.updatePointLog(pointLog);
        return Result.success();
    }

    @Operation(summary = "获取积分变动记录列表", description = "根据memberId获取积分变动记录")
    @ApiResponse(responseCode = "200", description = "查询成功")
    @GetMapping("page")
    public PageResult<PointLogListVO> page(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "状态 0:增加 1:减少") @RequestParam(required = false) Integer status) {
        log.info("积分变动查询参数：{},{},{}", current, pageSize, status);
        return PageResult.success(pointLogService.pageQuery(current, pageSize, status));
    }

    @Operation(summary = "获取用户总积分", description = "根据memberId获取用户总积分")
    @GetMapping("getTotalPoint")
    public Result<Integer> getTotalPoint() {
        log.info("用户总积分：{}",pointLogService.getTotalPoint());
        return Result.success(pointLogService.getTotalPoint());
    }

    @Operation(summary = "创建用户积分变动的记录", description = "在用户做各种行为时，创建用户消耗或获得积分的记录")
    @GetMapping("createPointLogWhenMemberAct")
    public Result<Boolean> createPointLogWhenMemberAct(
            @RequestParam("value") Integer value,
            @RequestParam("status") Integer status,
            @RequestParam("intro") String intro){
        log.info("value:{}", value);
        return Result.success(pointLogService.createPointLogWhenMemberAct(value, status, intro));
    }
}
